﻿using FlexFlow.Base;
using FlexFlow.Model;
using FlexFlow.Runtime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FlexFlow.MVC.Controllers
{
    /// <summary>
    /// 表示用户数据API控制器
    /// </summary>
    [Auth(AuthType.API)]
    public class UserDataAPIController : Controller
    {

        /// <summary>
        /// 取得当前登录用户上传的所有文件
        /// </summary>
        /// <param name="path">
        /// 要获取的文件所在的目录
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public ActionResult GetFiles(string path = "UploadFiles")
        {
            Guid uploader = DefaultComponentSettings.OnlineUserPool.GetLoginedUserInfo(Request).UserId;
            OperateResult res = UserDataManager.Default.GetFiles(uploader, path);
            if (res.ResultCode == 200)
            {
                List<FileDescription> fileList = (List<FileDescription>)res.Tag;
                List<object> resList = new List<object>();
                foreach (var file in fileList)
                {
                    resList.Add(new
                    {
                        id = file.Identity,
                        fileName = file.FileName,
                        size = file.Size,
                        uploadTime = file.UploadTime.ToString(),
                        outdateTime = file.OutdateTime.ToString(),
                        tag = file.Tag
                    });
                }
                return Json(new
                {
                    draw = 1,
                    recordsTotal = resList.Count,
                    recordsFiltered = resList.Count,
                    data = resList
                });
            }
            else
            {
                return Json(res);
            }
        }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns>
        /// 操作结果
        /// </returns>
        public ActionResult UploadFile(string path = "UploadFiles")
        {
            for (int i = 0; i < Request.Files.Count; i++)
            {
                var file = Request.Files[i];

                FileDescription des = DefaultComponentSettings.FileDescriptionProvider.Create();
                des.FileName = file.FileName;
                des.Size = file.ContentLength;
                des.UploadTime = DateTime.Now;
                des.Uploader = DefaultComponentSettings.OnlineUserPool.GetLoginedUserInfo(Request).UserId;
                des.Path = path;

                OperateResult res = UserDataManager.Default.UploadFile(des, file.InputStream);
                if (res.ResultCode != 200)
                {
                    Response.StatusCode = 500;
                    return Content(res.Message);
                }
            }
            return Content("");
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="fileId">
        /// 要下载的文件ID
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public ActionResult DownloadFile(Guid fileId)
        {
            FileDescription des;
            OperateResult res = UserDataManager.Default.DownloadFile(fileId, Response.OutputStream, out des);
            if (res.ResultCode == 200)
            {
                Response.AddHeader("Content-Disposition", "attachment;filename=" + des.FileName);
                Response.Flush();
                Response.End();
                return new EmptyResult();
            }
            else
            {
                return Json(res, JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        /// 删除具有指定ID的文件
        /// </summary>
        /// <param name="fileDesId">
        /// 要删除的文件ID
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public ActionResult DeleteFile(Guid fileDesId)
        {
            return Json(UserDataManager.Default.DeleteFile(fileDesId));
        }

    }
}
